package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.common.Result;
import com.example.domain.MFile;
import com.example.service.MFileService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("upload")
@Api("文件上传")
public class UploadFileController {


    private static String UPLOAD_PATH_PREFIX="upload";

    @Autowired
    private MFileService uFileService;

    @ResponseBody
    @RequestMapping(value="/multifileUpload",method= RequestMethod.POST)
    public JSONObject multifileUpload(MultipartFile file, HttpServletRequest request){
        if(file==null){
            return Result.errorObject("文件不存在");
        }
        //文件名
        String fileName = file.getOriginalFilename();
        //文件类型
        String type=file.getContentType();
        String realPath = new String("src/main/resources/" + UPLOAD_PATH_PREFIX);
        //文件大小
        Double size = Double.valueOf(String.valueOf(file.getSize()/1024*512)) ;

        File file1 = new File(realPath );
        if(!file1.isDirectory()){
            file1.mkdirs();
        }
        try {
            File dest = new File(file1.getAbsoluteFile() + File.separator+fileName);
            file.transferTo( dest ); //保存文件
            //文件路径
            String path=dest.getPath();
            MFile mFile=MFile.builder().filename(fileName).build();
            Boolean flag=uFileService.save(mFile);
            if(flag){
                return Result.successObject("上传成功");
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.errorObject("上传出错");
    }

    @RequestMapping(value="/uploadFile",method={RequestMethod.POST})
    @ResponseBody
    public Map<String,Object> addImg(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> result= new HashMap<>();

        // 转型为MultipartHttpRequest：
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        // 获得文件：
        MultipartFile file= multipartRequest.getFile("file_data");
        //文件名
        String fileName = file.getOriginalFilename();
        //文件类型
        String type=file.getContentType();
        String realPath = new String("src/main/resources/" + UPLOAD_PATH_PREFIX);
        //文件大小
        Long size1=(file.getSize()/1024)/1024;
        Double size = Double.valueOf(String.valueOf(size1)) ;

        File file1 = new File(realPath );
        if(!file1.isDirectory()){
            file1.mkdirs();
        }
        try {
            File dest = new File(file1.getAbsoluteFile() + File.separator+fileName);
            file.transferTo( dest ); //保存文件
            //文件路径
            String path=dest.getPath();
            MFile mFIle=MFile.builder().filesize(size)
                    .filetype(type).filename(fileName).build();
            Boolean flag=uFileService.save(mFIle);
            if(flag){
                return Result.successObject("上传成功");
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.errorObject("上传出错");

    }
}
